﻿<%@ Page Language="C#" MasterPageFile="~/Common.Master" AutoEventWireup="true" CodeBehind="SingleWellDataManage.aspx.cs" Inherits="Web.Views.DataManage.SingleWellDataManage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
	<title></title>
    <script type="text/javascript" src="../../Javascript/json2.js"></script>
    <script type="text/javascript" src="../../Easy_UI/MyJs/jquery-1.7.2.min.js"></script>
	<script type="text/javascript" src="../../Easy_UI/MyJs/jquery.easyui.min.js"></script> <%--jQuery EasyUI 1.3--%>
    <%--<script type="text/javascript" src="../../Easy_UI/jquery.easyui.min.js"></script>--%> <%--jQuery EasyUI 1.2.6--%>
    <script type="text/javascript">
        $(function () {
            $('#tbWellData').width($('body').width());
            $('#tbWellData').height($('body').height());
        });
        function newGuid() {
            var guid = "";
            for (var i = 1; i <= 32; i++) {
                var n = Math.floor(Math.random() * 16.0).toString(16);
                guid += n;
                if ((i == 8) || (i == 12) || (i == 16) || (i == 20))
                    guid += "-";
            }
            return guid;
        }
    </script>
    <script type="text/javascript">
        $(function () {
            var queryParams = $.parseJSON($(window.parent.document).find('#queryParams').value);
            var tableId = 'tbWellData';
            var tableJQuerySelector = '#tbWellData';
            var lastIndex;
            var idField = 'DataId';
            $(tableJQuerySelector).datagrid({
                queryParams:queryParams,
                frozenColumns: [[
                              { field: 'tbWellData_ck', checkbox: true },
                              { field: 'tbWellData_opt', title: '状态', width: 48, align: 'center',
                                  formatter: function (value, row, index) {
                                      var rowStatusIconId = 'rowStatusIcon_' + row[idField];
                                      return '<span style="vertical-align:middle;" id="' + rowStatusIconId + '"><img style="width:16px;height:16px;" src="icons/noChange.png" alt="未修改"><img style="width:16px;height:16px;display:none;" src="icons/edit.png" alt="已修改"><img style="width:16px;height:16px;display:none;" src="icons/add.png" alt="新增"></span>'
                                  }
                              }
				]],
                toolbar: [{
                    text: '新增',
                    iconCls: 'icon-add',
                    handler: function () {
                        $(tableJQuerySelector).datagrid('endEdit', lastIndex);
                        if (lastIndex > -1) {
                            var rows = $(tableJQuerySelector).datagrid('getRows');
                            if (lastIndex < rows.length) {
                                var lastRow = rows[lastIndex];
                                var validateResult = validateRow(lastRow);
                                if (!validateResult) {
                                    $(tableJQuerySelector).datagrid('beginEdit', lastIndex);
                                    return;
                                }
                            }
                        }
                        var tempNewId = newGuid();
                        $(tableJQuerySelector).datagrid('appendRow', {
                            DataId: tempNewId,
                            WellCode: null,
                            DataFileName: '',
                            WorkMode: '',
                            A4: null,
                            A3: null,
                            A0: null,
                            BeginDate: null,
                            FinishDate: null,
                            WellType: null,
                            BeginWorkDate: null,
                            FinishWorkDate: null,
                            WellDepth: null,
                            ArchiveCode: '',
                            QuanZongCode: ''
                        });
                        var rowStatusIconId = 'rowStatusIcon_' + tempNewId;
                        $('#' + rowStatusIconId + '>img:nth-child(1)').hide();
                        $('#' + rowStatusIconId + '>img:nth-child(2)').hide();
                        $('#' + rowStatusIconId + '>img:nth-child(3)').show();
                        lastIndex = $(tableJQuerySelector).datagrid('getRows').length - 1;
                        var tempLastIndex = lastIndex;
                        $(tableJQuerySelector).datagrid('selectRow', tempLastIndex);
                        $(tableJQuerySelector).datagrid('beginEdit', tempLastIndex);
                    }
                }, '-', {
                    text: '编辑',
                    iconCls: 'icon-edit',
                    handler: function () {
                        var row = $(tableJQuerySelector).datagrid('getSelected');
                        if (row == null) {
                            alert('请先选择！');
                            return;
                        }
                        var rowIndex = $(tableJQuerySelector).datagrid('getRowIndex', row);
                        if (lastIndex != rowIndex) {
                            $(tableJQuerySelector).datagrid('endEdit', lastIndex);
                            $(tableJQuerySelector).datagrid('beginEdit', rowIndex);
                        }
                        lastIndex = rowIndex;
                    }
                }, '-', {
                    text: '删除',
                    iconCls: 'icon-remove',
                    handler: function () {
                        var row = $(tableJQuerySelector).datagrid('getSelected');
                        if (row == null) {
                            alert('请先选择！');
                            return;
                        }
                        var index = $(tableJQuerySelector).datagrid('getRowIndex', row);
                        $(tableJQuerySelector).datagrid('deleteRow', index);
                        lastIndex = -1;
                    }
                }, '-', {
                    text: '提交',
                    iconCls: 'icon-save',
                    handler: function () {
                        $(tableJQuerySelector).datagrid('endEdit', lastIndex);
                        if (lastIndex > -1) {
                            var rows = $(tableJQuerySelector).datagrid('getRows');
                            if (lastIndex < rows.length) {
                                var lastRow = rows[lastIndex];
                                var validateResult = validateRow(lastRow);
                                if (!validateResult) {
                                    $(tableJQuerySelector).datagrid('beginEdit', lastIndex);
                                    return;
                                }
                            }
                        }
                        var insertedRows = $(tableJQuerySelector).datagrid('getChanges', 'inserted');
                        var updatedRows = $(tableJQuerySelector).datagrid('getChanges', 'updated');
                        var deletedRows = $(tableJQuerySelector).datagrid('getChanges', 'deleted');
                        var updateModel = new Object;
                        updateModel.InsertedRows = insertedRows;
                        updateModel.UpdatedRows = updatedRows;
                        updateModel.DeletedRows = deletedRows;
                        var postObj = new Object;
                        postObj.updateModel = JSON.stringify(updateModel);
                        var jsonStr;
                        if (insertedRows.length >= 1 || updatedRows.length >= 1 || deletedRows.length >= 1) {
                            try {
                                jsonStr = JSON.stringify(postObj);
                            }
                            catch (e) {
                                alert(e.Message);
                                return;
                            }
                            $.ajax({
                                type: 'POST',
                                contentType: "application/json",
                                url: "DataManageServices.asmx/UpdateSingleWellData",
                                data: jsonStr,
                                dataType: 'json',
                                success: function (event, XMLHttpRequest, ajaxOptions) {
                                    $(tableJQuerySelector).datagrid('acceptChanges');
                                    alert('更新成功！');
                                    return;
                                },
                                error: function (event, XMLHttpRequest, ajaxOptions, thrownError) {
                                    var message = '服务器错误！';
                                    response = jQuery.parseJSON(event.responseText);
                                    if (response != null) {
                                        message += ('</br>' + response.Message)
                                    }
                                    alert(message);
                                    return;
                                }
                            });
                        }
                        else {
                            alert('没有修改！');
                            return;
                        }
                    }
                }, '-',{
                    text: '撤销',
                    iconCls: 'icon-redo',
                    handler: function () {
                        $(tableJQuerySelector).datagrid('rejectChanges');
                    }
                }],
                onBeforeLoad: function (param) {
                    $(this).datagrid('rejectChanges');
                    lastIndex = -1;
                },
                onLoadError: function (event) {
                    var message = '加载失败！';
                    response = jQuery.parseJSON(event.responseText);
                    if (response != null) {
                        message += ('</br>' + response.Message)
                    }
                    alert(message);
                },
                onClickRow: function (rowIndex, rowData) {
                    if (lastIndex != rowIndex) {
                        $(tableJQuerySelector).datagrid('endEdit', lastIndex);
                        $(tableJQuerySelector).datagrid('unselectAll');
                        if (lastIndex > -1) {
                            var rows = $(tableJQuerySelector).datagrid('getRows');
                            if (lastIndex < rows.length) {
                                var lastRow = rows[lastIndex];
                                var validateResult = validateRow(lastRow);
                                if (!validateResult) {
                                    $(tableJQuerySelector).datagrid('selectRow', lastIndex);
                                    $(tableJQuerySelector).datagrid('beginEdit', lastIndex);
                                } else {
                                    $(tableJQuerySelector).datagrid('selectRow', rowIndex);
                                    lastIndex = -1;
                                }
                            }
                        } else {
                            $(tableJQuerySelector).datagrid('selectRow', rowIndex);
                            lastIndex = -1;
                        }
                    }
                },
                onAfterEdit: function (rowIndex, rowData, changes) {
                    var updatedRows = $(tableJQuerySelector).datagrid('getChanges', 'updated');
                    var insertedRows = $(tableJQuerySelector).datagrid('getChanges', 'inserted');
                    for (i = updatedRows.length - 1; i >= 0; i--) {
                        if (updatedRows[i][idField] == rowData[idField]) {
                            var rowStatusIconId = 'rowStatusIcon_' + rowData[idField];
                            $('#' + rowStatusIconId + '> img:nth-child(1)').hide();
                            $('#' + rowStatusIconId + '> img:nth-child(2)').show();
                            $('#' + rowStatusIconId + '> img:nth-child(3)').hide();
                            return;
                        }
                    }
                    for (i = insertedRows.length - 1; i >= 0; i--) {
                        if (insertedRows[i][idField] == rowData[idField]) {
                            var rowStatusIconId = 'rowStatusIcon_' + rowData[idField];
                            $('#' + rowStatusIconId + '>img:nth-child(1)').hide();
                            $('#' + rowStatusIconId + '>img:nth-child(2)').hide();
                            $('#' + rowStatusIconId + '>img:nth-child(3)').show();
                            return;
                        }
                    }
                }
            });
            var p = $(tableJQuerySelector).datagrid('getPager');
            $(p).pagination({
                pageSize: 10,
                pageList: [10, 20, 50],
                beforePageText: '第',
                afterPageText: '页    共 {pages} 页',
                displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录'
            });
        });

        function validateRow(rowData) {
            var msg = '';
            var flag = true;
            if(rowData.DataId=='' || rowData.DataId==null){
                msg += 'DataId不能为空<br/>';
                flag = false;
            }
            if (rowData.WellCode == '' || rowData.WellCode == null) {
                msg += '井号不能为空';
                flag = false;
            }
            if (rowData.DataFileName == '' || rowData.DataFileName == null) {
                msg += '文件名不能为空';
                flag = false;
            }
            if (msg != '') {
                alert(msg);
            }
            return flag;
        }
    </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <div>
        <table id="tbWellData"  data-options="singleSelect:true,idField:'DataId',url:'SingleWellDataManageHandler.ashx',pagination:true,rownumbers:true" title="单井数据维护">
		    <thead>
			    <tr>
				    <th data-options="field:'DataId',width:80,resizable:true">DataId</th>
                    <th data-options="field:'WellCode',width:80,resizable:true,editor:'text'">井号</th>
                    <th data-options="field:'DataFileName',width:80,resizable:true,editor:'text'">文件名</th>
                    <th data-options="field:'WorkMode',width:80,resizable:true,editor:'text'">施工措施</th>
                    <th data-options="field:'A4',width:80,resizable:true,editor:{type:'numberbox'}">A4</th>
                    <th data-options="field:'A3',width:80,resizable:true,editor:{type:'numberbox'}">A3</th>
                    <th data-options="field:'A0',width:80,resizable:true,editor:{type:'numberbox'}">A0</th>
                    <th data-options="field:'BeginDate',width:80,resizable:true,editor:{type:'datebox'}">开工日期</th>
                    <th data-options="field:'FinishDate',width:80,resizable:true,editor:{type:'datebox'}">完工日期</th>
                    <th data-options="field:'WellType',width:80,resizable:true,editor:'text'">井别</th>
                    <th data-options="field:'BeginWorkDate',width:80,resizable:true,editor:{type:'datebox'}">开钻日期</th>
                    <th data-options="field:'FinishWorkDate',width:80,resizable:true,editor:{type:'datebox'}">完钻日期</th>
                    <th data-options="field:'WellDepth',width:80,resizable:true,editor:{type:'numberbox'}">完钻井深</th>
                    <th data-options="field:'ArchiveCode',width:80,resizable:true,editor:'text'">档号</th>
                    <th data-options="field:'QuanZongCode',width:80,resizable:true,editor:'text'">全宗号</th>
			    </tr>
		    </thead>
	    </table>
    </div>
</asp:Content>

